package config

import (
	"fmt"

	"gitee.com/ddlin/kit/xlog"
	"gitee.com/ddlin/kit/xrds"
	"gitee.com/ddlin/tiktok-go-tool/service"
	"github.com/spf13/viper"
)

type TxBucketConfig struct {
	Url       string `json:"url"`
	SecretId  string `json:"secretId"`
	SecretKey string `json:"secretKey"`
}

type Config struct {
	Mode            string                        `toml:"mode" json:"mode"`
	Bind            string                        `toml:"bind" json:"bind"`
	Name            string                        `toml:"name" json:"name"`
	Version         string                        `toml:"version" json:"version"`
	HttpClient      map[string]service.HttpConfig `toml:"httpClient" json:"httpClient"`
	XLogger         xlog.Config                   `toml:"xlogger" json:"xlogger"`
	HkBucketConfig  TxBucketConfig                `toml:"hkBucketConfig" json:"hkBucketConfig"` // tx桶配置
	Domain          string                        `toml:"domain" json:"domain"`                 // 域名
	XRedis          xrds.Config                   `toml:"xredis" json:"xredis"`                 // redis配置
	DownlaodUrlPath string                        `toml:"downlaodUrlPath" json:"downlaodUrlPath"`
}

func ParseConfig() Config {
	viper.SetConfigName("config")
	viper.SetConfigType("json")
	viper.AddConfigPath("./config")
	viper.AddConfigPath("/home/go/tiktok-go-tool-hk-config/")
	viper.AddConfigPath("../../config")

	err := viper.ReadInConfig()
	if err != nil {
		panic(fmt.Sprintf("Fatal error config file: %s", err))
	}

	var cfg Config
	err = viper.Unmarshal(&cfg)

	if err != nil {
		// 处理读取配置文件的错误
		panic(fmt.Sprintf("Unmarshal error config file: %s", err))
	}

	return cfg
}
